home *** CD-ROM | disk | FTP | other *** search
- /*
- File: OTLookupNameTest.c
-
- Contains: A simple illustration of how to use OTLookupName.
-
- Written by: Quinn "The Eskimo!"
-
- Copyright: Copyright © 1997-1999 by Apple Computer, Inc., All Rights Reserved.
-
- You may incorporate this Apple sample source code into your program(s) without
- restriction. This Apple sample source code has been provided "AS IS" and the
- responsibility for its operation is yours. You are not permitted to redistribute
- this Apple sample source code as "Apple sample source code" after having made
- changes. If you're going to re-distribute the source, we require that you make
- it clear in the source that the code was descended from Apple sample source
- code, but that you've made changes.
-
- Change History (most recent first):
- 7/22/1999 Karl Groethe Updated for Metrowerks Codewarror Pro 2.1
-
-
- */
-
- /////////////////////////////////////////////////////////////////////
- // The OT debugging macros in <OTDebug.h> require this variable to
- // be set.
- #include <Events.h>
- #ifndef qDebug
- #define qDebug 1
- #endif
-
- /////////////////////////////////////////////////////////////////////
- // Pick up all the standard OT stuff.
-
- #include <OpenTransport.h>
-
- /////////////////////////////////////////////////////////////////////
- // Pick up all the OT AppleTalk stuff.
-
- #include <OpenTptAppleTalk.h>
-
- /////////////////////////////////////////////////////////////////////
- // Pick up the OTDebugBreak and OTAssert macros.
-
- #include <OTDebug.h>
-
- /////////////////////////////////////////////////////////////////////
- // Standard C prototypes.
-
- #include <stdio.h>
-
- /////////////////////////////////////////////////////////////////////
- // OTDebugStr is not defined in any OT header files, but it is
- // exported by the libraries, so we define the prototype here.
-
- extern pascal void OTDebugStr(const char* str);
-
- /////////////////////////////////////////////////////////////////////
-
- static UInt32 gLastPrinted = 0;
-
- static pascal void YieldingNotifier(EndpointRef ep, OTEventCode code,
- OTResult result, void* cookie)
- // This notifier calls printf on a period basis in response
- // to a kOTSyncIdleEvent. By the wonders of SIOUX, printf calls
- // WaitNextEvent for us, and hence our synchronous calls to
- // OT will yield to other processes.
- {
- #pragma unused(ep)
- #pragma unused(result)
- #pragma unused(cookie)
-
- switch (code) {
- case kOTSyncIdleEvent:
- if (TickCount() > gLastPrinted + 10) {
- printf(".");
- fflush(stdout);
- gLastPrinted = TickCount();
- }
- break;
- default:
- // do nothing
- break;
- }
- }
-
- /////////////////////////////////////////////////////////////////////
-
- static void PrintAddress( DDPAddress *addr )
- // Prints a DDPAddress in a nicely formatted manner.
- {
- OTAssert( "PrintAddress: Expected a DDPNBPADdress", addr->fAddressType == AF_ATALK_DDP );
- printf("Net = $%04x, Node = $%02x, Socket = $%02x ",
- addr->fNetwork,
- addr->fNodeID,
- addr->fSocket);
- }
-
- static void PrintName(const char *name, UInt32 length)
- // Prints on NBP name in a nicely formatted manner.
- {
- char nameForPrinting[256];
-
- OTMemzero(nameForPrinting, 256);
- OTMemcpy(nameForPrinting, name, length);
-
- printf("“%s”", nameForPrinting);
- }
-
- enum {
- kResponseBufferSize = 8192
- };
-
- static OSStatus LookupAndPrint(char *requestAddress)
- // requestAddress must be an NBP address of the form
- // <name>:<type>@<zone>. This routine opens an NBP mapper
- // provider, switches it into synchronous/blocking mode
- // and uses sync idle events to yield time to other processes.
- // It then issues an NBP lookup request using OTLookupName.
- // When the request completes, it prints out the resulting
- // addresses and names.
- {
- OSStatus err;
- OSStatus junk;
- MapperRef nbpMapper;
- TLookupRequest lookupRequest;
- TLookupReply lookupReply;
- UInt8 *responseBuffer;
- TLookupBuffer *currentLookupReplyBuffer;
- UInt32 nameIndex;
-
- err = noErr;
- nbpMapper = kOTInvalidMapperRef;
-
- // Create the responseBuffer.
-
- responseBuffer = OTAllocMem(kResponseBufferSize);
- if (responseBuffer == nil) {
- err = kENOMEMErr;
- }
-
- // Create an NBP mapper and set it to up for threaded processing.
-
- if (err == noErr) {
- nbpMapper = OTOpenMapper(OTCreateConfiguration(kNBPName), 0, &err);
- }
- if (err == noErr) {
- junk = OTSetSynchronous(nbpMapper);
- OTAssert("LookupAndPrint: Could not set synchronous mode on mapper", junk == noErr);
- junk = OTSetBlocking(nbpMapper);
- OTAssert("LookupAndPrint: Could not set blocking mode on mapper", junk == noErr);
- junk = OTUseSyncIdleEvents(nbpMapper, true);
- OTAssert("LookupAndPrint: Could not enable sync idle events on mapper", junk == noErr);
- junk = OTInstallNotifier(nbpMapper, YieldingNotifier, nil);
- OTAssert("LookupAndPrint: Could not install notifier into mapper", junk == noErr);
- }
-
- // Issue the OTLookupName synchronously.
-
- if (err == noErr) {
-
- // Set up the TLookupRequest structure.
-
- OTMemzero(&lookupRequest, sizeof(lookupRequest));
- lookupRequest.name.buf = (UInt8 *) requestAddress;
- lookupRequest.name.len = OTStrLength(requestAddress);
- lookupRequest.timeout = 1000; // 1 second in milliseconds
- lookupRequest.maxcnt = kResponseBufferSize / kNBPEntityBufferSize;
-
- // Set up the TLookupReply structure.
-
- OTMemzero(&lookupReply, sizeof(lookupReply));
- lookupReply.names.buf = responseBuffer;
- lookupReply.names.maxlen = kResponseBufferSize;
-
- // Call OT synchronously.
-
- err = OTLookupName(nbpMapper, &lookupRequest, &lookupReply);
- }
-
- // Print out the contents of the responseBuffer.
-
- if (err == noErr) {
- printf("\n");
-
- // Start by pointing currentLookupReplyBuffer to point to the
- // beginning of the response buffer.
-
- currentLookupReplyBuffer = (TLookupBuffer *) responseBuffer;
-
- // For each response in the buffer...
-
- for (nameIndex = 0; nameIndex < lookupReply.rspcount; nameIndex++) {
-
- // ... print the name and address and...
-
- printf("%3d ", nameIndex);
- PrintAddress( (DDPAddress *) ¤tLookupReplyBuffer->fAddressBuffer[0]);
- PrintName( (char *) ¤tLookupReplyBuffer->fAddressBuffer[currentLookupReplyBuffer->fAddressLength], currentLookupReplyBuffer->fNameLength);
- printf("\n");
-
- // ... use OTNextLookupBuffer to get from the current buffer to the next.
-
- currentLookupReplyBuffer = OTNextLookupBuffer(currentLookupReplyBuffer);
- }
- }
-
- // Clean up.
-
- if (responseBuffer != nil) {
- OTFreeMem(responseBuffer);
- }
- if (nbpMapper != kOTInvalidMapperRef) {
- junk = OTCloseProvider(nbpMapper);
- OTAssert("LookupAndPrint: Failed closing mapper", junk == noErr);
- }
-
- return (err);
- }
-
- /////////////////////////////////////////////////////////////////////
-
- void main(void)
- {
- OSStatus err;
- char requestAddress[] = "=:AFPServer@*";
-
- printf("Hello Cruel World!\n");
-
- err = InitOpenTransport();
-
- if (err == noErr) {
-
- err = LookupAndPrint(requestAddress);
-
- CloseOpenTransport();
- }
-
- if (err == noErr) {
- printf("Success.\n");
- } else {
- printf("Failed with error %d.\n", err);
- }
- printf("Done. Press command-Q to Quit.\n");
- }
-